package ex02.pyrmont;

import com.sky.JULLog.MyLogger;

import java.io.OutputStream;
import java.io.IOException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.File;
import java.io.PrintWriter;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletResponse;
import javax.servlet.ServletOutputStream;


/**
 * 升级版的 response
 */
public class ResponseV1 implements ServletResponse {


    private static Logger log = MyLogger.getLogger(ResponseV1.class);

    private static final int BUFFER_SIZE = 1024;
    RequestV1 request;
    OutputStream output;
    PrintWriter writer;

    protected ResponseV1(OutputStream output) {
        this.output = output;
    }

    protected void setRequest(RequestV1 request) {
        this.request = request;
    }

    /* This method is used to serve a static page */
    protected void sendStaticResource() throws IOException {
        byte[] bytes = new byte[BUFFER_SIZE];
        FileInputStream fis = null;
        String errorMessage = "";
        try {
            /* request.getUri has been replaced by request.getRequestURI */
            File file = new File(Constants.WEB_ROOT, request.getUri());

            if (file.exists()) {


                fis = new FileInputStream(file);
                int ch = fis.read(bytes, 0, BUFFER_SIZE);
                while (ch != -1) {
                    output.write(bytes, 0, ch);
                    ch = fis.read(bytes, 0, BUFFER_SIZE);
                }
            } else if (TrashyTomcatV1.SHUTDOWN_COMMAND.equals(request.getUri())) {

                String shutdownMsg = "HTTP/1.1 200 OK\r\n" +
                        "Content-Type: text/html\r\n" +
                        "Content-Length: " + "<h1>serverShutDown............</h1>".getBytes().length + "\r\n" +   //长度, 一定要对应上, 否则 Firefox 会读不完data ;
                        "\r\n" +
                        "<h1>serverShutDown............</h1>";   //内容
                output.write(shutdownMsg.getBytes());

            } else {
                errorMessage = "HTTP/1.1 404 File Not Found\r\n" +
                        "Content-Type: text/html\r\n" +
                        "Content-Length: 23\r\n" +
                        "\r\n" +
                        "<h1>File Not Found</h1>";
                output.write(errorMessage.getBytes());

            }

        } catch (FileNotFoundException e) {
            log.log(Level.SEVERE, e.getMessage());
            errorMessage = "HTTP/1.1 500 Internal Error\r\n" +
                    "Content-Type: text/html\r\n" +
                    "Content-Length: " + ("<h1> " + e.getMessage() + "</h1>").getBytes().length + "\r\n" +
                    "\r\n" +
                    "<h1> " + e.getMessage() + "</h1>";
            output.write(errorMessage.getBytes());
        } finally {
            if (fis != null) {
                fis.close();
            }
        }
    }


    /**
     * implementation of ServletResponse mark 这里设置 httpResponse 的输出流;
     */
    @Override
    public PrintWriter getWriter() throws IOException {
        // auto flush is true, println() will flush, but print() will not.
        writer = new PrintWriter(output, true);
        return writer;
    }

    @Override
    public String getCharacterEncoding() {
        return null;
    }

    @Override
    public ServletOutputStream getOutputStream() throws IOException {
        return null;
    }

    @Override
    public void setContentLength(int i) {

    }

    @Override
    public void setContentType(String s) {

    }

    @Override
    public void setBufferSize(int i) {

    }

    @Override
    public int getBufferSize() {
        return 0;
    }

    @Override
    public void flushBuffer() throws IOException {

    }

    @Override
    public void resetBuffer() {

    }

    @Override
    public boolean isCommitted() {
        return false;
    }

    @Override
    public void reset() {

    }

    @Override
    public void setLocale(Locale locale) {

    }

    @Override
    public Locale getLocale() {
        return null;
    }
}
